package com.objetdirect.gwt.umldrawer.client;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.Panel;

public class WorkSpacePage implements PageContent {

	private VerticalPanel workSpacePanel = new VerticalPanel();
	private VerticalPanel workSpaceBody = new VerticalPanel();
	private HorizontalPanel leftAndRightSides = new HorizontalPanel();
	private VerticalPanel leftSidePanel = new VerticalPanel();
	private VerticalPanel rightSidePanel = new VerticalPanel();
	
	/*
	 * Left Side
	 */
	private Panel profilePicturePanel = new Panel();
	private Label myProfile = new Label();
	private Label myProjects = new Label();
	private Label inbox = new Label();
	private Label notifications = new Label();
	private Label deactivateAccount = new Label();
	
	private Image myProfileIcon = new Image();
	private Image myProjectsIcon = new Image();
	private Image inboxIcon = new Image();
	private Image notificationsIcon = new Image();
	private Image deactivateAccountIcon = new Image();
	
	private Image profilePic = new Image();
	private Image header = new Image();
	private Image footer = new Image();
	private Label welcome = new Label();
	
	EditProfile editProfile = new EditProfile();
	
	private static HorizontalPanel currentSelected = new HorizontalPanel();
	
	@Override
	public void setPageContent() {
		header.setUrl("images/header.png");
		header.setWidth("1201px");
		header.setStyleName("headerImage");
		
		footer.setUrl("images/footer.png");
		footer.setWidth("1201px");
		
		/*
		 * Left Side Panel
		 */
		welcome.setText("Welcome");
		welcome.setStyleName("pagesHeader");
		
		profilePic.setUrl("profilePics/unavailable.jpg");
		profilePic.setStyleName("profilePic");
		
		profilePicturePanel.setTitle("Full Name Here");
		profilePicturePanel.setCollapsible(true);
		profilePicturePanel.setTitleCollapse(true);
		profilePicturePanel.setWidth(200);
		profilePicturePanel.add(profilePic);
		profilePicturePanel.setFrame(true);
		profilePicturePanel.setStyleName("profilePicturePanel");
		
		Button changePicture = new Button("Change Picture");
		profilePicturePanel.setBottomToolbar(changePicture);
		
		VerticalPanel leftSideMenu = new VerticalPanel();

		//My Projects
		final HorizontalPanel myProjectsPanel = new HorizontalPanel();
		
		myProjectsIcon.setUrl("images/projects.png");
		myProjectsIcon.setStyleName("myProjectsMenuIcon");
		myProjects.setText("My Projects");
		initializeMenuItem(myProjectsPanel, "MyProjects", myProjects, myProjectsIcon);
		
		myProjects.addMouseOverHandler(new MouseOverHandler() {
			@Override
			public void onMouseOver(MouseOverEvent event) {
				if (isSelected(myProjectsPanel) == false)
					highlightOnMouseOver(myProjectsPanel);
			}
		});
		
		myProjects.addMouseOutHandler(new MouseOutHandler() {
			@Override
			public void onMouseOut(MouseOutEvent event) {
				if (isSelected(myProjectsPanel) == false)
					highlightOnMouseOut(myProjectsPanel);
			}
		});
		
		myProjects.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				if (isSelected(myProjectsPanel) == false) {
					highlightOnSelect(myProjectsPanel);
					changeRightSidePanel(new MyProjectsWorkSpace().getMyProjects(), myProjectsPanel);
				}
			}
		});
		leftSideMenu.add(myProjectsPanel);
		
		/**
		 * Load The Projects First ...
		 */
		highlightOnSelect(myProjectsPanel);
		changeRightSidePanel(new MyProjectsWorkSpace().getMyProjects(), myProjectsPanel);
		
		//My Profile
		final HorizontalPanel myProfilePanel = new HorizontalPanel();
		
		myProfileIcon.setUrl("images/profile.png");
		myProfileIcon.setStyleName("myProfileMenuIcon");
		myProfile.setText("My Profile");
		initializeMenuItem(myProfilePanel, "My Profile", myProfile, myProfileIcon);
		
		myProfile.addMouseOverHandler(new MouseOverHandler() {
			@Override
			public void onMouseOver(MouseOverEvent event) {
				if (isSelected(myProfilePanel) == false)
					highlightOnMouseOver(myProfilePanel);
			}
		});
		
		myProfile.addMouseOutHandler(new MouseOutHandler() {
			@Override
			public void onMouseOut(MouseOutEvent event) {
				if (isSelected(myProfilePanel) == false)
					highlightOnMouseOut(myProfilePanel);
			}
		});
		
		myProfile.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				if (isSelected(myProfilePanel) == false) {
					highlightOnSelect(myProfilePanel);
					changeRightSidePanel(editProfile.getEditProfilePanel() , myProfilePanel);
					//Bakhoum
				}
			}
		});
		leftSideMenu.add(myProfilePanel);
		
		//In box
		final HorizontalPanel inboxPanel = new HorizontalPanel();

		inboxIcon.setUrl("images/inbox.png");
		inboxIcon.setStyleName("myInboxMenuItem");
		inbox.setText("Inbox");
		initializeMenuItem(inboxPanel, "My Inbox", inbox, inboxIcon);
		
		inbox.addMouseOverHandler(new MouseOverHandler() {
			@Override
			public void onMouseOver(MouseOverEvent event) {
				if (isSelected(inboxPanel) == false)
					highlightOnMouseOver(inboxPanel);
			}
		});
		
		inbox.addMouseOutHandler(new MouseOutHandler() {
			@Override
			public void onMouseOut(MouseOutEvent event) {
				if (isSelected(inboxPanel) == false)
					highlightOnMouseOut(inboxPanel);
			}
		});
		
		inbox.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				if (isSelected(inboxPanel) == false) {
					highlightOnSelect(inboxPanel);
					changeRightSidePanel(new InboxPage().getInbox(), inboxPanel);
				}
			}
		});
		
		leftSideMenu.add(inboxPanel);
		
		//Notifications
		final HorizontalPanel notificationsPanel = new HorizontalPanel();
		
		notificationsIcon.setUrl("images/notifications.png");
		
		notifications.setText("Notifications");
		initializeMenuItem(notificationsPanel, "My Notifications", notifications, notificationsIcon);
		
		notifications.addMouseOverHandler(new MouseOverHandler() {
			@Override
			public void onMouseOver(MouseOverEvent event) {
				if (isSelected(notificationsPanel) == false)
					highlightOnMouseOver(notificationsPanel);
			}
		});
		
		notifications.addMouseOutHandler(new MouseOutHandler() {
			@Override
			public void onMouseOut(MouseOutEvent event) {
				if (isSelected(notificationsPanel) == false)
					highlightOnMouseOut(notificationsPanel);
			}
		});
		notifications.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				if (isSelected(notificationsPanel) == false) {
					highlightOnSelect(notificationsPanel);
					changeRightSidePanel(new Label("notifications"), notificationsPanel);
				}
			}
		});
		leftSideMenu.add(notificationsPanel);
		
		//Delete Account
		final HorizontalPanel deactivateAccountPanel = new HorizontalPanel();
		
		deactivateAccountIcon.setUrl("images/skull.png");
		deactivateAccountIcon.setStyleName("deactivateAccountMenuIcon");
		deactivateAccount.setText("Deactivate Account");
		initializeMenuItem(deactivateAccountPanel, "Deactivate My Account", deactivateAccount, deactivateAccountIcon);
		
		deactivateAccount.addMouseOverHandler(new MouseOverHandler() {
			@Override
			public void onMouseOver(MouseOverEvent event) {
				if (isSelected(deactivateAccountPanel) == false)
					highlightOnMouseOver(deactivateAccountPanel);
			}
		});
		
		deactivateAccount.addMouseOutHandler(new MouseOutHandler() {
			@Override
			public void onMouseOut(MouseOutEvent event) {
				if (isSelected(deactivateAccountPanel) == false)
					highlightOnMouseOut(deactivateAccountPanel);
			}
		});
		
		deactivateAccount.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				if (isSelected(deactivateAccountPanel) == false) {
					highlightOnSelect(deactivateAccountPanel);
					changeRightSidePanel(new Label(), deactivateAccountPanel);
				}
			}
		});
		leftSideMenu.add(deactivateAccountPanel);
		
		leftSideMenu.setStyleName("leftSideMenu");
		
		leftSidePanel.add(welcome);
		leftSidePanel.add(profilePicturePanel);
		leftSidePanel.add(leftSideMenu);
		
		leftSidePanel.setStyleName("leftSidePanel");
		
		//------------------------ End Of Left Side Panel ---------------------------------------
		
		rightSidePanel.setStyleName("rightSidePanel");
		
		leftAndRightSides.add(leftSidePanel);
		leftAndRightSides.add(rightSidePanel);
		
		workSpaceBody.add(new HeaderButtons().getHeaderButtons());
		workSpaceBody.add(leftAndRightSides);
		workSpaceBody.add(new PrivacyBar().getprivacyBar());
		workSpaceBody.setStyleName("workSpaceBody");
		
		workSpacePanel.add(header);
		workSpacePanel.add(workSpaceBody);
		workSpacePanel.add(footer);
	}

	@Override
	public VerticalPanel getPageContent() {
		setPageContent();
		return workSpacePanel;
	}
	
	public void initializeMenuItem(HorizontalPanel panel, String title, Label text, Image icon) {
		
		panel.setWidth("200px");
		panel.setStyleName("menuMouseOut");
		panel.setTitle(title);
		panel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		panel.add(icon);
		panel.add(text);
	}
	
	public void highlightOnMouseOver(HorizontalPanel panel) {
		panel.setStyleName("menuMouseOver");
	}
	
	public void highlightOnMouseOut(HorizontalPanel panel) {
		panel.setStyleName("menuMouseOut");
	}
	
	public boolean isSelected(HorizontalPanel panel) {
		if (currentSelected.equals(panel)) {
			return true;
		}
		else
			return false;
	}
	
	public void highlightOnSelect(HorizontalPanel panel) {
		panel.setStyleName("selectedMenuItem");
	}
	
	/**
	 * Remove previous highlighted item, highlight the selected one, and change the content.
	 * @param widget
	 * @param newSelectedItem
	 */
	public void changeRightSidePanel (Widget newContent, HorizontalPanel newSelectedItem) {
		currentSelected.setStyleName("menuMouseOut");
		currentSelected = newSelectedItem;
		rightSidePanel.clear();
		rightSidePanel.add(newContent);
	}
}
